package com.wanglibao.woodtrade.woodtrade.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanglibao.woodtrade.woodtrade.R;
import com.wanglibao.woodtrade.woodtrade.model.impl.UserImpl;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.MyCSOrderFragment;
import com.wanglibao.woodtrade.woodtrade.ui.fragment.MyGMOrderFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
/*
* 我的订单界面 可以查看 出售订单 和 购买订单
*
* */

public class MyOrderActivity extends FragmentActivity implements View.OnClickListener {


    @Bind(R.id.imgbtn_commonTitle_back_my_order)
    ImageButton mImgbtnCommonTitleBackMyOrder;
    @Bind(R.id.tv_chushou_title_my_order_activity)
    TextView mTvChushouTitleMyOrderActivity;
    @Bind(R.id.tv_qiugou_title_my_order_activity)
    TextView mTvQiugouTitleMyOrderActivity;
    @Bind(R.id.tv_title_top_activity)
    TextView mTvTitleTopActivity;
    @Bind(R.id.tv_title_bottom_activity)
    TextView mTvTitleBottomActivity;
    @Bind(R.id.tv_baiban_left_my_order_activity)
    TextView mTvBaibanLeftMyOrderActivity;
    @Bind(R.id.tv_baiban_right_my_order_activity)
    TextView mTvBaibanRightMyOrderActivity;
    @Bind(R.id.rl_title_my_order_activity)
    RelativeLayout mRlTitleMyOrderActivity;
    @Bind(R.id.tv_select_all_my_order_activity)
    TextView mTvSelectAllMyOrderActivity;
    @Bind(R.id.tv_select_dqr_my_order_activity)
    TextView mTvSelectDqrMyOrderActivity;
    @Bind(R.id.tv_select_yqr_my_order_activity)
    TextView mTvSelectYqrMyOrderActivity;
    @Bind(R.id.tv_select_ywc_my_order_activity)
    TextView mTvSelectYwcMyOrderActivity;
    @Bind(R.id.ll_select_my_order_activity)
    LinearLayout mLlSelectMyOrderActivity;
    @Bind(R.id.vp_my_order_activity)
    ViewPager mVpMyOrderActivity;
    //=====================================
    List<Fragment> mFragmentList = new ArrayList<>();
    MyCSOrderFragment mMyCSOrderFragment;
    MyGMOrderFragment mMyGMOrderFragent;
    boolean isFristClick = false;
    int position1 = 4;
    int position2 = 4;
    int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= 19) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        } else {
            mRlTitleMyOrderActivity.setPadding(0, 0, 0, 0);
        }
        initData();
        System.out.println(new UserImpl(getApplicationContext()).getUser().token);
    }

    private void initData() {
        mImgbtnCommonTitleBackMyOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mMyCSOrderFragment = new MyCSOrderFragment();
        mMyGMOrderFragent = new MyGMOrderFragment();
        mFragmentList.add(mMyGMOrderFragent);
        mFragmentList.add(mMyCSOrderFragment);
        FragAdapter fragAdapter = new FragAdapter(getSupportFragmentManager(), mFragmentList);
        mVpMyOrderActivity.setAdapter(fragAdapter);
        mVpMyOrderActivity.setOffscreenPageLimit(1);
        mVpMyOrderActivity.addOnPageChangeListener(new MyOnPageChangeListener());
        mTvSelectAllMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
        mTvQiugouTitleMyOrderActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mVpMyOrderActivity.setCurrentItem(0);
            }
        });
        mTvSelectAllMyOrderActivity.setOnClickListener(this);
        mTvSelectDqrMyOrderActivity.setOnClickListener(this);
        mTvSelectYqrMyOrderActivity.setOnClickListener(this);
        mTvSelectYwcMyOrderActivity.setOnClickListener(this);
        mTvChushouTitleMyOrderActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mVpMyOrderActivity.setCurrentItem(1);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_select_all_my_order_activity:
                if (type == 0) {
                    position1 = 4;
                    selectButton(type,position1);
                } else {
                    position2 = 4;
                    selectButton(type,position2);
                }
                break;
            case R.id.tv_select_dqr_my_order_activity:
                if (type == 0) {
                    position1 = 0;
                    selectButton(type,position1);
                } else {
                    position2 = 0;
                    selectButton(type,position2);
                }
                break;
            case R.id.tv_select_yqr_my_order_activity:
                if (type == 0) {
                    position1 = 1;
                    selectButton(type,position1);
                } else {
                    position2 = 1;
                    selectButton(type,position2);
                }
                break;
            case R.id.tv_select_ywc_my_order_activity:
                if (type == 0) {
                    position1 = 2;
                    selectButton(type,position1);
                } else {
                    position2 = 2;
                    selectButton(type,position2);
                }
                break;
            default:
                break;
        }

    }


    class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            if (position == 0) {
                type = 0;
                //购买界面
                mMyGMOrderFragent.NetWorking(4);
                mTvSelectAllMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
                mTvSelectDqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mTvSelectYqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mTvSelectYwcMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mTvBaibanLeftMyOrderActivity.setVisibility(View.VISIBLE);
                mTvQiugouTitleMyOrderActivity.setTextColor(getResources().getColor(R.color.white));
                mTvBaibanRightMyOrderActivity.setVisibility(View.INVISIBLE);
                mTvChushouTitleMyOrderActivity.setTextColor(getResources().getColor(R.color.unSelect));
            } else {
                type = 1;
                //出售界面
                mMyCSOrderFragment.NetWorking(4);
                mTvSelectAllMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
                mTvSelectDqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mTvSelectYqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mTvSelectYwcMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
                mTvBaibanLeftMyOrderActivity.setVisibility(View.INVISIBLE);
                mTvQiugouTitleMyOrderActivity.setTextColor(getResources().getColor(R.color.unSelect));
                mTvBaibanRightMyOrderActivity.setVisibility(View.VISIBLE);
                mTvChushouTitleMyOrderActivity.setTextColor(getResources().getColor(R.color.white));
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }

    }

    public void selectButton(int type, int position) {
        if (position == 4) {
            mTvSelectAllMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
            mTvSelectDqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectYqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectYwcMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
        }
        if (position == 0) {
            mTvSelectDqrMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
            mTvSelectAllMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectYqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectYwcMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));

        }
        if (position == 1) {
            mTvSelectYqrMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
            mTvSelectAllMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectDqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectYwcMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));

        }
        if (position == 2) {
            mTvSelectYwcMyOrderActivity.setTextColor(getResources().getColor(R.color.orange));
            mTvSelectAllMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectDqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
            mTvSelectYqrMyOrderActivity.setTextColor(getResources().getColor(R.color.text_color_didian));
        }
        if (type == 0) {
            mMyGMOrderFragent.NetWorking(position);
        } else if (type == 1) {
            mMyCSOrderFragment.NetWorking(position);
        }
    }

    class FragAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragments;

        public FragAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }
}
